﻿/*
 * Author: Mattan360
 * Contributors:
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Irrlicht.Net.Video;

namespace Irrlicht.Net.Video
{
    public sealed class VideoDriver : ReferenceCounted
    {
        #region PInvoke
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern bool Driver_beginScene(IntPtr driver, bool backBuffer, bool zBuffer,
	        uint a, uint r, uint g, uint b);
        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern bool Driver_endScene(IntPtr driver);

        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr Driver_getTexture(IntPtr scmgr,
            [MarshalAs(UnmanagedType.AnsiBStr)]
            string path);

        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern int Driver_getFPS(IntPtr driver);

        [DllImport(@"IrrlichtW.dll", CallingConvention = CallingConvention.Cdecl)]
        internal static extern IntPtr Driver_getName(IntPtr driver);
        #endregion

        internal static VideoDriver CreateVideoDriver(IntPtr pointer)
        {
            VideoDriver driver = new VideoDriver();
            driver.pointer = pointer;
            return driver;
        }

        public string Name
        {
            get
            {
                IntPtr point = Driver_getName(pointer);
                return Marshal.PtrToStringUni(point);
            }
        }

        public int FPS
        {
            get
            {
                return Driver_getFPS(pointer);
            }
        }

        public bool BeginScene()
        {
            return Driver_beginScene(pointer, true, true, 255, 0, 0, 0);
        }

        public bool BeginScene(Color color)
        {
            return Driver_beginScene(pointer, true, true, color.Alpha, color.Red, color.Green, color.Blue);
        }

        public bool BeginScene(bool backBuffer, bool zBuffer, Color color)
        {
            return Driver_beginScene(pointer, backBuffer, zBuffer, color.Alpha, color.Red, color.Green, color.Blue);
        }

        public bool EndScene()
        {
            return Driver_endScene(pointer);
        }

        public Texture GetTexture(string path)
        {
            return new Texture(Driver_getTexture(pointer, path));
        }
    }
}
